package com.example.baseutils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
import com.example.baseutils.application.BaseApplication
import java.lang.RuntimeException

/**
 * Author by lp,on 2020/8/28/028,15:17.
 * 常用的工具类
 */

//比较系列数据大小
fun <T : Comparable<T>> max(vararg nums: T): T {
    if (nums.isEmpty()) throw RuntimeException("参数不能为空")
    var maxNum = nums[0]
    for (num in nums) {
        if (maxNum < num) {
            maxNum = num
        }
    }
    return maxNum
}


//弹出提示框
fun String.showToast(context: Context=BaseApplication.instance, duration: Int = Toast.LENGTH_LONG) {
    Toast.makeText(context, this, duration).show()
}

//日志
fun String.log(tag: String, logLevel: LogUtils.LogLevel = LogUtils.LogLevel.I) {
    when (logLevel) {
        LogUtils.LogLevel.I -> {
            Log.i(tag, this)
        }
        else -> {
            LogUtils.log(tag, this, logLevel)
        }
    }
}
//日志
fun String.log(location: String) {
    Log.i(location, this)

}
fun Activity.start(context: Context, block: Intent.() -> Unit) {
    val intent = Intent(context, this::class.java)
    intent.block()
    context.startActivity(intent)
}

fun Activity.start(context: Context) {
    val intent = Intent(context, this::class.java)
    context.startActivity(intent)
}
fun Activity.startResult(activity: Activity,requestCode:Int) {
    val intent = Intent(activity, this::class.java)
    activity.startActivityForResult(intent,requestCode)
}
fun Activity.startResult(activity: Activity,requestCode:Int,block: Intent.() -> Unit) {
    val intent = Intent(activity, this::class.java)
    intent.block()
    activity.startActivityForResult(intent,requestCode)
}

/**
 * 集合的深拷贝，赋值的对象必须实现Serializable
 * */
fun <T>List<T>.deepCopy():List<T>{
    return ListUtlils.deepCopy(this)?: ArrayList()
}

